from flask_script import Manager
from flask_migrate import MigrateCommand, Migrate
from models.model import db
from app import app

from flask import request, jsonify, g
from utils.MyJwt import myjwt
from utils.myredis import r

# 创建黑窗口
manage = Manager(app)
# 实例化迁移
migrate = Migrate(app, db)
# 给黑窗口添加命令
manage.add_command("db", MigrateCommand)




# 钩子函数
# @app.before_request
# def gz():
#     pathlist = ["/user/login", "/user/smscole", "/user/imagecode", "/user/weibo", "/user/weiboCallback/", "/user/bindMobile", '/static']
#     # 获取token
#     path = request.path
#     print("path---：",path)
#     # print(path not in pathlist)
#
#     if path not in pathlist:
#         if path[:7] not in pathlist:
#             try:
#                 # print("??????????????????????????",request.headers["token"],"?")
#                 token = request.headers["token"]
#                 # 是否过期
#                 flag = myjwt.valid(token)
#                 if not flag:
#                     return jsonify({"code": 403, "msg": "登录过期"})
#                 # token有没有被改变
#                 check_token = myjwt.check_token(token)
#                 if not check_token:
#                     return jsonify({"code": 403, "msg": "token被篡改"})
#                 # 从队列中读取所有的key,如果token在队列中，直接返回此token已经退出，不能操作
#                 pkey = "tokenlist"
#                 res = r.zset_getall(pkey)
#                 for i in res:
#                     if token in i.jwt_decode():
#                         return jsonify({"code": 403, "msg": "此token已经退出，不能操作"})
#                     # else:
#                     #     # 解析token
#                     #     return jsonify({"code": 200, "msg": "请求成功"})
#                 payload = myjwt.jwt_decode(token)
#                 g.uid = payload["id"]
#             except Exception as e:
#                 print(e)
#                 return jsonify({"code": 403, "msg": "你无权访问"})


if __name__ == '__main__':
    manage.run(
        # host='127.0.0.1',
        # post=8000,
        # debug=True
    )

